<section class="content-header">
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li><a href="#">订单管理</a></li>
        <li class="active">订单详情</li>
    </ol>
</section>
<!-- Main content -->
<section class="content">
    <div class="jax-box">
        <form id="formSearch" class="form-horizontal form-search">
            <div class="form-group">
                <div class="col-md-4 col-sm-6">
                    <label class="control-label  col-xs-4" for="odrCustId">用户id:</label>
                    <div class="col-xs-8">
                        <input type="text" class="form-control" placeholder="" id="odrCustId">
                    </div>
                </div>
                <div class="col-md-4 col-sm-6">
                    <label class="control-label  col-xs-4" for="odrCustName">用户名:</label>
                    <div class="col-xs-8">
                        <input type="text" class="form-control" placeholder="" id="odrCustName">
                    </div>
                </div>
                <shiro:hasPermission name="user:list">
                    <div class="col-md-1 col-sm-6 " style="text-align:left;">
                        <button type="button" id="btn_query" class="btn btn-info">查询</button>
                    </div>
                </shiro:hasPermission>
            </div>
        </form>
    </div>
    <div class="jax-box jax-box-table">
        <div id="toolbar" class="btn-group">
            <shiro:hasPermission name="cstOrder:add">
                <button id="btn_add" type="button" class="btn btn-primary" data-toggle="modal" data-target="#userModal">
                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
                </button>
            </shiro:hasPermission>
            <shiro:hasPermission name="cstOrder:batchDelete">
                <button id="btn_batch_delete" type="button" class="btn btn-danger">
                    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>批量删除
                </button>
            </shiro:hasPermission>
        </div>
        <table id="table"></table>
    </div>
</section>
<!-- 用户新增模态框 -->
<div id="userModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <!--        user新增按钮页面-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">新增订单</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="cstOrderForm">
                    <div class="box-body">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">客户编号 <span style="color: red">*</span>:</label>
                            <div class="col-sm-8">
                                <input name="odrCustId" class="form-control" require="必填项" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">客户名 <span style="color: red">*</span>:</label>
                            <div class="col-sm-8">
                                <input name="odrCustName" class="form-control" require="必填项" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">订单所在地址 <span
                                    style="color: red">*</span>:</label>

                            <div class="col-sm-8">
                                <input name="odrAddress" class="form-control" placeholder="">
                            </div>
                        </div>


                    </div>
                    <div class="box-footer">
                        <div class="pull-right">
                            <button type="button" id="saveUser" class="btn btn-info">确定</button>
                            <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<!--&lt;!&ndash; 用户详情模态框 &ndash;&gt;-->
<div id="userDetailModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">订单信息</h4>
            </div>
            <div class="modal-body">
                <div id="userOpenWindow">
                </div>
            </div>
        </div>
    </div>
</div>
<!-- 付款模态框 -->
<div id="payModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <!--        user新增按钮页面-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">付款页面</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="payForm">
                    <div class="box-body">
                        <div class="form-group" hidden>
                            <label class="col-sm-3 control-label" for="svrCreateId">创建人编号</label>
                            <div class="col-sm-8">
                                <input name="svrCreateId" id="svrCreateId" class="form-control" type="text"
                                       require="必填项" placeholder="" readonly/>
                                <span id="svr-Create-Id" hidden><shiro:principal property="userId"/></span>
                            </div>
                        </div>
                        <div class="form-group" hidden>
                            <label class="col-sm-3 control-label" for="svrCreateName">创建人姓名</label>
                            <div class="col-sm-8">
                                <input name="svrCreateName" id="svrCreateName" class="form-control" type="text"
                                       require="必填项" placeholder="" readonly/>
                                <span id="svr-Create-Name" hidden><shiro:principal property="username"/></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label" for="odrOrder">订单编号</label>
                            <div class="col-sm-8">
                                <input name="odrOrder" id="odrOrder" class="form-control" type="text"
                                       require="必填项" placeholder="" readonly/>
                            </div>
                        </div>
                        <div class="form-group" hidden>
                            <label class="col-sm-3 control-label" for="svrCustId">客户编号</label>
                            <div class="col-sm-8">
                                <input name="svrCustId" id="svrCustId" class="form-control" type="text"
                                       require="必填项" placeholder="" readonly/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label" for="svrCustName">客户姓名</label>
                            <div class="col-sm-8">
                                <input name="svrCustName" id="svrCustName" class="form-control" type="text"
                                       require="必填项" placeholder="" readonly/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label" for="svrTitle">备注</label>
                            <div class="col-sm-8">
                                <input name="svrTitle" id="svrTitle" class="form-control" min="1" type="text"
                                       require="必填项" placeholder=""/>
                            </div>
                        </div>
                    </div>
                    <div class="box-footer">
                        <div class="pull-right">
                            <button type="button" id="savePay" class="btn btn-info">确定</button>
                            <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    var editFlag = "[[${@perms.hasPerm('cstOrder:edit')}]]";
    var deleteFlag = "[[${@perms.hasPerm('cstOrder:delete')}]]";
    // var assignRoleFlag = "[[${@perms.hasPerm('user:assignRole')}]]";
    var odrIdStr;//批量用户id
    var roleIdStr;//分配的角色
    // var selectRoleIds;
    var userIdChecked;
    // var loadRoleCount = 0;
    var columns = [
        {checkbox: true},
        {
            field: 'odrCustName',
            title: '订单人',
            align: "center"
        }, {
            field: 'odrOrder',
            title: '订单号',
            align: "center"
        }, {
            field: 'odrDate',
            title: '订单时间',
            align: "center"
        }, {
            field: 'odrAddress',
            title: '所在地址',
            align: "center",
        }, {
            field: 'odrStatus',
            title: '订单状态',
            align: "center",
            formatter: function (value, row, index) {
                return row.odrStatus == '1' ? "<p style='color: green'>保障中</p>" : "<p style='color: red'>待支付</p>";
            }
        },
        {
            field: 'operation',
            title: '操作',
            align: "center",
            formatter: function (value, row, index) {
                console.log(row.odrId);
                var del;
                var edit = editFlag == "true" ? '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="editUser(' + row.odrId + ')">订单详情</a>' : '';
                if (row.odrStatus == '1') {
                    del = "<a  class='table-btn table-btn-success' href='javascript:void(0)'>服务</a>";
                } else {
                    del = "<a  class='table-btn table-btn-danger' href='javascript:void(0)' onclick='pay(" + JSON.stringify(row) + ")'>付款</a>";
                }
                return edit + del;
            }
        }];
    var options = {
        id: "#table",
        url: '/cstOrder/list',
        columns: columns,
        toolbar: '#toolbar',
        showRefresh: true,
        queryParams: queryParams
    }
    Core.initTable(options);
    /*查询用户参数*/
    function queryParams(params) {
        var custId = "[[${param.odrCustId}]]";
        var custName = "[[${param.odrCustName}]]";
        var temp = { //这里的键的名字和控制器的变量名必须一致，这边改动，控制器也需要改成一样的
            limit: params.limit, //页面大小
            offset: params.offset, //页码
            odrStatus: $("#status").val(),
            odrCustId: custId ? custId : $("#odrCustId").val(),
            odrCustName: custName ? custName : $("#odrCustName").val(),
        };
        return temp;
    }
    // /*编辑*/
    function editUser(odrId) {
        Core.load("#userOpenWindow", "/cstOrder/edit?odrId=" + odrId, function () {
            $("#userDetailModal").modal("show");
        }, 2);
    }
    /*删除*/
    function deleteUser(odrId) {
        Core.confirm("确定删除该用户？", function () {
            Core.postAjax("/cstOrder/delete", {"odrId": odrId}, function (data) {
                if (data.status == 200) {
                    Core.refreshTable("#table");
                }
                layer.msg(data.msg);
            }, "get")
        })
    }
    // 付款模态框赋值
    function pay(val) {
        console.log("付款");
        console.log(val);
        var id = $("#svr-Create-Id").html();
        var name = $("#svr-Create-Name").html();
        $("#odrOrder").val(val.odrOrder);
        $("#svrCustId").val(val.odrCustId);
        $("#svrCustName").val(val.odrCustName);
        $("#svrCreateId").val(id);
        $("#svrCreateName").val(name);
        $("#payModal").modal("show");
    }
    $(function () {
        // 保存付款
        $("#savePay").click(function () {
            var pay = $("#payForm").serialize();
            console.log("保存付款",pay);
            Core.postAjax("/cstOrder/pay", pay, function (data){
                console.log(data);
                if(data.status==200){
                    Core.refreshTable("#table");
                    $("#payModal").modal("hide");
                    clear("#payModal");
                    layer.msg(data.msg);
                }else{
                    layer.msg(data.msg);
                }
            },"get")
        })
        /*查询*/
        $("#btn_query").click(function () {
            Core.refreshTable("#table");
        });
        // 新增
        $("#btn_add").click(function () {
            Core.clearError("#cstOrderForm");
        });
        /*批量删除*/
        $("#btn_batch_delete").click(function () {
            var checkedRows = Core.selectMutiData("#table");
            if (checkedRows) {
                Core.confirm("确定删除选中的" + checkedRows.length + "条记录？", function () {
                    odrIdStr = "";
                    $.each(checkedRows, function (i, item) {
                        odrIdStr += (item.odrId + ",");
                    })
                    odrIdStr = odrIdStr.substring(0, odrIdStr.length - 1);
                    Core.postAjax("/cstOrder/batch/delete", {"odrIdStr": odrIdStr}, function (data) {
                        if (data.status == 200) {
                            Core.refreshTable("#table");
                        }
                        layer.msg(data.msg);
                    }, "get")
                })
            }
        });
        /*保存用戶 新增 确定*/
        $("#saveUser").click(function () {
            if (doValidForm(cstOrderForm)) {
                Core.mask("#saveUser");
                Core.postAjax("/cstOrder/add", $("#cstOrderForm").serialize(), function (data) {
                    Core.unmask("#saveUser");
                    if (data.status == 200) {
                        $("#userModal").modal("hide");
                        $("#cstOrderForm")[0].reset();
                        Core.refreshTable("#table")
                    }
                    layer.msg(data.msg);
                })
            }
        });
        //清空表单内数据
        function clear(modal) {
            $(modal).find("form").trigger("reset");
        }
    });

</script>